package cn.itcast.zt.routers;

import cn.itcast.zt.handlers.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * 类似于springMVC的dispatcherServlet
 * created by zhangtian on 2018/7/9
 */
@Configuration
public class AllRouters {
    @Bean
    public RouterFunction<ServerResponse> userRouter(UserHandler handler){
        // path 相当于之前类上面的@RequestMapping注解
        // route 相当于类里面的@RequestMapping @GetMapping等等
        return RouterFunctions.nest(RequestPredicates.path("/user"),
                RouterFunctions.route(
                            // 查询所有用户
                            RequestPredicates.GET("/"),
                            handler::getAllUser
                        )
                        // 创建用户
                        .andRoute(
                                RequestPredicates.POST("/").and(RequestPredicates.accept(MediaType.APPLICATION_JSON_UTF8)),
                                handler::createUser
                        )
                        // 删除用户
                        .andRoute(
                                RequestPredicates.DELETE("{id}"),handler::deleteUserById
                        )
        ) ;
    }
}
